<template>
  <div class="student-guidance-admin">
    <div class="page-header">
      <h1 class="page-title">人才培养审核</h1>
      <div class="header-actions">
        <el-button type="primary" @click="fetchList">刷新</el-button>
      </div>
    </div>

    <el-card class="filter-card">
      <div class="filter-bar">
        <el-input v-model="filters.teacherName" placeholder="教师姓名" clearable style="width: 140px;" />
        <el-input v-model="filters.teacherId" placeholder="教师ID" clearable style="width: 120px; margin-left: 12px;" />
        <el-select v-model="filters.studentType" placeholder="学生类型" clearable style="width: 140px; margin-left: 12px;">
          <el-option label="本科生" value="本科生" />
          <el-option label="硕士生" value="硕士生" />
          <el-option label="博士生" value="博士生" />
        </el-select>
        <el-select v-model="filters.achievementType" placeholder="成果类型" clearable style="width: 140px; margin-left: 12px;">
          <el-option label="竞赛" value="获奖" />
          <el-option label="项目" value="项目" />
          <el-option label="论文" value="论文" />
        </el-select>
        <el-input v-model="filters.level" placeholder="级别/分区" clearable style="width: 160px; margin-left: 12px;" />
        <el-input v-model="filters.year" placeholder="年份" clearable style="width: 100px; margin-left: 12px;" />
        <el-select v-model="filters.status" placeholder="状态" clearable style="width: 120px; margin-left: 12px;">
          <el-option label="待审核" value="PENDING" />
          <el-option label="已通过" value="APPROVED" />
          <el-option label="已驳回" value="REJECTED" />
        </el-select>
        <el-button type="primary" style="margin-left: 12px;" @click="fetchList">查询</el-button>
      </div>
    </el-card>

    <el-row :gutter="12" class="stats-row">
      <el-col :span="6"><el-card class="stat-card"><div class="stat-num">{{ stats.totalGuidance }}</div><div class="stat-label">指导记录数</div></el-card></el-col>
      <el-col :span="6"><el-card class="stat-card"><div class="stat-num">{{ stats.totalAchievements }}</div><div class="stat-label">成果总数</div></el-card></el-col>
      <el-col :span="6"><el-card class="stat-card"><div class="stat-num">{{ stats.awardCount }}</div><div class="stat-label">获奖</div></el-card></el-col>
      <el-col :span="6"><el-card class="stat-card"><div class="stat-num">{{ stats.paperCount }}</div><div class="stat-label">论文</div></el-card></el-col>
    </el-row>

    <el-card>
      <el-table :data="rows" stripe v-loading="loading">
        <el-table-column prop="user.name" label="教师" width="100">
          <template #default="{ row }">{{ row.user?.name || '-' }}</template>
        </el-table-column>
        <el-table-column prop="studentName" label="学生姓名" width="120" />
        <el-table-column prop="studentType" label="学生类型" width="100" />
        <el-table-column prop="guidanceCategory" label="指导内容" width="120" />
        <el-table-column label="起止" width="220">
          <template #default="{ row }">{{ d(row.startDate) }} ~ {{ d(row.endDate) }}</template>
        </el-table-column>
        <el-table-column prop="guidanceTopic" label="主题/课题" min-width="160" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }"><el-tag :type="tag(row.status)">{{ st(row.status) }}</el-tag></template>
        </el-table-column>
        <el-table-column label="操作" width="220" fixed="right">
          <template #default="{ row }">
            <el-button type="text" @click="openReview(row, 'APPROVED')">通过</el-button>
            <el-button type="text" style="color:#f56c6c;" @click="openReview(row, 'REJECTED')">驳回</el-button>
            <el-button type="text" @click="view(row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div style="text-align:right;margin-top:12px;">
        <el-pagination background layout="prev, pager, next, jumper" :page-size="pageSize" :current-page="page" :total="total" @current-change="onPage" />
      </div>
    </el-card>

    <!-- 审核对话框 -->
    <el-dialog v-model="showReview" title="审核指导记录" width="520px">
      <el-form :model="reviewForm" label-width="80px">
        <el-form-item label="审核结果">
          <el-radio-group v-model="reviewForm.status">
            <el-radio label="APPROVED">已通过</el-radio>
            <el-radio label="REJECTED">已驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见">
          <el-input type="textarea" v-model="reviewForm.opinion" rows="3" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showReview=false">取消</el-button>
        <el-button type="primary" :loading="reviewing" @click="submitReview">确定</el-button>
      </template>
    </el-dialog>

    <!-- 查看对话框 -->
    <el-dialog v-model="showView" title="指导详情" width="800px">
      <div v-if="current">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="教师">{{ current.user?.name || '-' }}</el-descriptions-item>
          <el-descriptions-item label="学生姓名">{{ current.studentName }}</el-descriptions-item>
          <el-descriptions-item label="学生类型">{{ current.studentType }}</el-descriptions-item>
          <el-descriptions-item label="指导内容">{{ current.guidanceCategory }}</el-descriptions-item>
          <el-descriptions-item label="起始">{{ d(current.startDate) }}</el-descriptions-item>
          <el-descriptions-item label="结束">{{ d(current.endDate) }}</el-descriptions-item>
          <el-descriptions-item label="审核状态"><el-tag :type="tag(current.status)">{{ st(current.status) }}</el-tag></el-descriptions-item>
          <el-descriptions-item label="审核意见">{{ current.reviewOpinion || '-' }}</el-descriptions-item>
        </el-descriptions>
        <el-divider content-position="left">成果</el-divider>
        <el-table :data="current.achievements||[]" border>
          <el-table-column prop="achievementType" label="类型" width="120" />
          <el-table-column prop="title" label="标题/名称" min-width="220" />
          <el-table-column prop="level" label="级别/分区" width="160" />
          <el-table-column prop="achievementDate" label="日期" width="120">
            <template #default="{ row }">{{ d(row.achievementDate) }}</template>
          </el-table-column>
        </el-table>
      </div>
      <template #footer>
        <el-button type="primary" @click="showView=false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import studentGuidanceApi from '@/api/studentGuidance'

const loading = ref(false)
const page = ref(1)
const pageSize = ref(10)
const total = ref(0)
const rows = ref([])
const filters = ref({ teacherId: '', teacherName: '', studentType: '', achievementType: '', level: '', year: '', status: '' })

const stats = ref({ totalGuidance: 0, totalAchievements: 0, awardCount: 0, paperCount: 0, projectCount: 0, excellentThesisCount: 0 })

const showReview = ref(false)
const reviewing = ref(false)
const reviewForm = ref({ id: null, status: 'APPROVED', opinion: '' })

const showView = ref(false)
const current = ref(null)

function d(v){ return String(v||'').substring(0,10) }
function st(s){ if(!s) return '待审核'; if(s==='APPROVED') return '已通过'; if(s==='REJECTED') return '已驳回'; return '待审核' }
function tag(s){ const t=st(s); return t==='已通过'?'success':(t==='已驳回'?'danger':'info') }

async function fetchList(){
  try{
    loading.value=true
    const params = {
      page: page.value, size: pageSize.value,
      teacherId: filters.value.teacherId || undefined,
      teacherName: filters.value.teacherName || undefined,
      studentType: filters.value.studentType || undefined,
      achievementType: filters.value.achievementType || undefined,
      level: filters.value.level || undefined,
      year: filters.value.year || undefined,
      status: filters.value.status || undefined
    }
    const res = await studentGuidanceApi.list(params)
    rows.value = res?.content || []
    total.value = res?.totalElements || 0
    const s = await studentGuidanceApi.stats({ teacherId: params.teacherId, studentType: params.studentType, achievementType: params.achievementType, level: params.level, year: params.year })
    stats.value = s || stats.value
  }catch(e){ console.error(e); ElMessage.error('获取数据失败') } finally { loading.value=false }
}

function onPage(p){ page.value=p; fetchList() }

function openReview(row, def){ reviewForm.value={ id: row.id, status: def || 'APPROVED', opinion: '' }; showReview.value=true }
async function submitReview(){
  try{ reviewing.value=true; await studentGuidanceApi.review(reviewForm.value.id, { status: reviewForm.value.status, opinion: reviewForm.value.opinion }); ElMessage.success('审核完成'); showReview.value=false; fetchList() }catch(e){ console.error(e); ElMessage.error('审核失败') } finally { reviewing.value=false }
}

async function view(row){ try{ current.value = await studentGuidanceApi.getById(row.id); showView.value=true }catch(e){ console.error(e) } }

onMounted(fetchList)
</script>

<style scoped>
.student-guidance-admin { max-width: 1400px; }
.page-header { display:flex; justify-content:space-between; align-items:center; margin-bottom:24px; }
.page-title { margin:0; color:#333; font-size:24px; font-weight:500; }
.filter-card { margin-bottom: 16px; }
.filter-bar { display:flex; align-items:center; flex-wrap:wrap; gap:12px; }
.stats-row { margin-bottom: 16px; }
.stat-card { text-align:center; }
.stat-num { font-size:24px; font-weight:600; color:#409eff; }
.stat-label { color:#666; }
</style> 